package cn.xuchunh.campusbbs.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseActivity;
import cn.xuchunh.campusbbs.ui.MainNavigationTab;
import cn.xuchunh.campusbbs.utils.Action;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;

/**
 * Created by XuChunH on 2015/9/9.
 */
public class MainActivity extends BaseActivity {

    private SearchView searchView;
    public static MainActivity instance;

    public static void launch(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView(View view) {
        FragmentTabHost fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.container);
        fragmentTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        MainNavigationTab[] tabs = MainNavigationTab.values();
        for (MainNavigationTab tabResource : tabs) {
            View indicator = getLayoutInflater().inflate(R.layout.layout_main_navigation_tab, null);
            TextView textView = (TextView) indicator.findViewById(R.id.main_navigation_icon);
            textView.setText(tabResource.getTitleRes());
            Drawable drawable = getResources().getDrawable(tabResource.getIconRes());
            textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            TabHost.TabSpec tab = fragmentTabHost.newTabSpec(getString(tabResource.getTitleRes()));
            tab.setIndicator(indicator);
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    return new View(MainActivity.this);
                }
            });
            fragmentTabHost.addTab(tab, tabResource.getClz(), null);
        }
        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                if (getString(MainNavigationTab.HOMEPAGE.getTitleRes()).equals(tabId)) {
                    ViewCompat.setElevation(getToolbar(), 0);
                } else {
                    ViewCompat.setElevation(getToolbar(), getResources().getDimensionPixelSize(R.dimen.elevation));
                }
            }
        });

    }

    @Override
    protected void beforeInitView() {
        instance = this;
    }

    @Override
    protected void afterInitView() {
        ViewCompat.setElevation(getToolbar(), 0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        final MenuItem menuItem = menu.findItem(R.id.menu_main_search);
        searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        searchView.setQueryHint(getString(R.string.search_hint));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (!TextUtils.isEmpty(query)) {
                    Intent intent = new Intent();
                    intent.setAction(Action.SEARCH);
                    intent.putExtra(IntentParamsKey.SEARCH_QUERY, query);
                    searchView.clearFocus();
                    menuItem.collapseActionView();
                    startActivity(intent);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.configuration) {
            SettingActivity.launch(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected int createContentRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onDestroy() {
        instance = null;
        AppContext.getInstance().exit();
        super.onDestroy();
    }
}
